<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/admin.css">
    <link rel="stylesheet" href="/static/md/css/editormd.css"/>
    <title>许久没道过早安的后台</title>
    <style>
        #input {
            width: 500px;
            margin-top: 20px;
        }

        #label {
            width: 800px;
        }

        #submit {
            /*float:right;*/
            margin-left: 50px;
        }
    </style>
</head>
<body>
<div class="body">
    <div class="layui-fluid">
        {include file='public/sider' /}
        <div id="main">
            {include file='public/header' /}

            <div class="layui-fluid" id="body">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <div class="layui-form">
                            <div id="input">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">
                                        文章标题
                                    </label>
                                    <div class="layui-input-block">
                                        <input type="text" name="title" placeholder="请输入" autocomplete="off"
                                               class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">
                                        文章类别
                                    </label>
                                    <div class="layui-input-block" id="label">
                                        {volist name="label" id="vo"}
                                        <input type="checkbox" name="label" value="{$vo.id}" title="{$vo.label}"
                                               lay-filter="label">
                                        {/volist}
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    文章内容
                                </label>

                                <div id="md">
                                    <textarea id="my-md-md" name="content" style="display:none;"></textarea>
                                </div>

                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    是否发布
                                </label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="status" value="1" title="发布">
                                </div>
                            </div>
                            <div id="submit">
                                <div class="layui-form-item">
                                    <button class="layui-btn" lay-submit lay-filter="form">提交</button>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script src="/static/jquery.min.js"></script>
<script src="/static/layui/layui.js"></script>
<script src="/static/md/editormd.min.js"></script>
<script>
    var height = window.screen.availHeight;
    document.getElementById('body').style.height = parseInt(height) - 190 + "px";
    document.getElementById('sider').style.height = parseInt(height) - 165 + "px";
    layui.use(['jquery', 'form', 'layer', 'layedit'], function () {
        var form = layui.form;
        var layer = layui.layer;
        // 采集复选框的值
        var checkbox = [];
        form.on('checkbox(label)', function (data) {
            var index = checkbox.indexOf(data.value);
            if (index != -1) checkbox.splice(index, 1);
            else checkbox.push(data.value);
        });
        $(function () {
            editormd("md", {
                width: "90%",
                height: 500,
                syncScrolling: "single",
                path: "/static/md/lib/",//注意2：你的路径
                saveHTMLToTextarea: true,//注意3：这个配置，方便post提交表单
                emoji: true,

                imageUpload: true,
                imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL: "{:url('common/upload')}",//注意你后端的上传图片服务地址

            });
        });


        // 监听表单提交
        form.on('submit(form)', function (data) {
            data.field['label'] = checkbox;
            $.ajax({
                async: true,
                type: "POST",
                url: "{:url('article/article_add')}",
                data: data.field,
                success: function (data) {
                    if (data.status == 200) {
                        layer.msg(data.msg);
                    } else {
                        layer.msg(data.msg);
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>